/**
 * File:   ExportStatisticsAction.java
 * Author: Gvennet
 */
package gui.actions;

import dht.node.DHTStatistics;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * todo write javadoc
 */
public final class ExportStatisticsAction extends AbstractAction
{

	private final DHTStatistics statistics;

	private final JFrame frame;

	private JFileChooser fileChooser;

	public ExportStatisticsAction(DHTStatistics statistics, JFrame frame)
	{
		super("Экспорт");
		putValue(SHORT_DESCRIPTION, "Экспорт");
		putValue(MNEMONIC_KEY, KeyEvent.VK_A);
		this.statistics = statistics;
		this.frame = frame;
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		if (fileChooser == null)
		{
			fileChooser = new JFileChooser();
			fileChooser.setMultiSelectionEnabled(false);
		}
		if (fileChooser.showSaveDialog(frame) == JFileChooser.APPROVE_OPTION)
		{
			String path = fileChooser.getSelectedFile().getAbsolutePath();
			try
			{
				PrintWriter output = new PrintWriter(new FileWriter(path));
				output.println("pingRequestIn\tpingResponseIn\t"
						+ "getNodesRequestIn\tgetNodesResponseIn\t"
						+ "putValueRequestIn\tputValueResponseIn\t"
						+ "getValueRequestIn\tgetValueResponseIn\tgetValueResponseNodesIn\t"
						+ "pingRequestOut\tpingResponseOut\t"
						+ "getNodesRequestOut\tgetNodesResponseOut\t"
						+ "putValueRequestOut\tputValueResponseOut\t"
						+ "getValueRequestOut\tgetValueResponseOut\tgetValueResponseNodesOut\t"
						+ "availability\tdata\tnodes");
				for (int[] s : statistics.getStatistics())
				{
					output.println(s[0] + "   " + s[1] + "   " + s[2] + "   " + s[3] + "   " + s[4] + "   " + s[5] + "   " + s[6] + "   " +
							s[7] + "   " + s[8] + "   " + s[9] + "   " + s[10] + "   " + s[11] + "   " + s[12] + "   " + s[13] + "   " +
							s[14] + "   " + s[15] + "   " + s[16] + "   " + s[17] + "   " + s[18] + "   " + s[19] + "   " + s[20]);
				}
				output.close();
			}
			catch (IOException e1)
			{
				e1.printStackTrace();
				System.out.println(":(");
			}
		}

	}
}

